package net.stancristian.restnews;

import java.text.SimpleDateFormat;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class NewsItemActivity extends Activity {

	private TextView ModificationDate;
	private TextView Title;
	private TextView Summary;
	private TextView Content;
	private Integer newsItemId;
	private NewsItem newsItem;
	private SimpleDateFormat myFormat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_item);

		ModificationDate = (TextView) findViewById(R.id.textViewModifictionDate);
		Title = (TextView) findViewById(R.id.textViewTitle);
		Summary = (TextView) findViewById(R.id.textViewSummary);
		Content = (TextView) findViewById(R.id.textViewContent);
		myFormat = new SimpleDateFormat("dd MMM yyyy");
		
		Bundle bundle = getIntent().getExtras();
		newsItemId = bundle.getInt("news_item_id");
		ShowNewsItem();
	}

	private void ShowNewsItem() {
		String baseApiAddress = getString(R.string.api_base_address);
		String Url = baseApiAddress + "/News/" + newsItemId.toString();

		HttpResponse response = RestNewsClient.getRequestWebService(Url);
		if (response != null
				&& response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			JSONObject object = RestNewsClient
					.GetJsonObjectFromResponse(response);
			newsItem = new NewsItem(object);
		}
		
		ModificationDate.setText(myFormat.format(newsItem.ModificationDate));
		Title.setText(newsItem.Title);
		Summary.setText(newsItem.Summary);
		Content.setText(newsItem.Content);
	}

}
